var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      imgselected:0,
      imgselected2:0,
      img1:'',
      img2:'',
      cardetail:'',
      id:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    if(id){
      this.setData({
        id: id
      })
      this.getaiche(id);
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 获取爱车详情
  getaiche: function (id) {
    var that = this;
    var param = {
      url: "api/common/carInfo",
      data: {
        car_id: id
      },
      success: function (e) {
        var imgselected;
        var imgselected2;
        e.data.data.drive_positive != '' ? imgselected = 1 : imgselected = 0;
        e.data.data.drive_reverse != '' ? imgselected2 = 1 : imgselected2 = 0;
        that.setData({
          cardetail: e.data.data,
          imgselected:imgselected,
          imgselected2:imgselected2,
          img1: e.data.data.drive_positive,
          img2: e.data.data.drive_reverse
        })
      }
    };
    app.post(param);
  },
  chooseimg:function(){
    var that = this;
    var success=function(res){
      var data = JSON.parse(res.data);
      that.setData({
        img1:data.data.url,
        imgselected:1
      })
    }
    app.uploadimg(success);
    
  },
  chooseimg2:function(){
    var that = this;
    var success = function (res) {
      var data = JSON.parse(res.data);
      that.setData({
        img2: data.data.url,
        imgselected2:1
      })
    }
    app.uploadimg(success);
  },
  delimg:function(){
    var that = this;
    wx.showModal({
      title: '提醒',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            img1:'',
            imgselected:0
          })
        } else if (res.cancel) {
          return false;
        }
      }
    })
  },
  delimg2:function(){
    var that = this;
    wx.showModal({
      title: '提醒',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            img2: '',
            imgselected2: 0
          })
        } else if (res.cancel) {
          return false;
        }
      }
    }) 
  },
  // 提交数据
  carform:function(e){
    var chepai = e.detail.value.chepai;
    var chexing = e.detail.value.chexing;
    var fadongji = e.detail.value.fadongji;
    var nianfen = e.detail.value.nianfen;
    var pailiang = e.detail.value.pailiang;
    var shibiehao = e.detail.value.shibiehao;
    if(chexing == ""){
        wx.showToast({
          title: '车型必填',
          icon:'none',
          duration:3000
        })
        return false;
    }
    var img1 = this.data.img1;
    var img2 = this.data.img2;
    var userid=wx.getStorageSync('userId');
    var that = this;
    if((img1!=''&&img2!= '')||(fadongji!=''&&chepai!=''&&shibiehao!='')){
          var datas = {
            user_id: userid,
            drive_positive: img1,
            drive_reverse: img2,
            kind: chexing,
            number: chepai,
            emission: pailiang,
            create_year: nianfen,
            car_identifier: shibiehao,
            engine_number: fadongji
          }
        if(that.data.id != ''){
          datas.id = that.data.id;
          var obj = {
            url: "api/user/editCar",
            data: datas,
            success: function (e) {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
              var pages = getCurrentPages();
              var beforePage = pages[pages.length - 2];
              beforePage.getaiche();
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000)
            },
            fail: function (e) {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            },
          }
          app.post(obj);

          
        }else{

          var obj = {
            url: "api/user/bindcar",
            data: datas,
            success: function (e) {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
              var pages = getCurrentPages();
              var beforePage = pages[pages.length - 2];
              beforePage.getaiche();
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000)
            },
            fail: function (e) {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            },
          }
          app.post(obj);
        }
    }else{
      wx.showToast({
        title:'请上传图片或填写表单',
        icon:'none',
        duration:3000
      })
    }


  }

})